#include "SplitFbx.h"
#include "Util.h"
#include <fstream>
#include <iostream>
using namespace fbxTool;
void SplitFbx::GetAllMeshNodes(FbxNode* node, vector<FbxNode*>& meshNodes)
{
	if (!node) return;

	// 获取节点属性
	FbxNodeAttribute* attr = node->GetNodeAttribute();

	// 如果节点属性为网格类型，加入到meshNodes中
	if (attr && attr->GetAttributeType() == FbxNodeAttribute::eMesh) {
		meshNodes.push_back(node);
	}

	// 遍历子节点并递归处理
	size_t numChildren = node->GetChildCount();
	for (size_t i = 0; i < numChildren; ++i) {
		// 递归遍历子节点
		GetAllMeshNodes(node->GetChild(static_cast<int>(i)), meshNodes);
	}
}

void SplitFbx::GenerateJsonFileList(vector<string>& files, const string& filename, const string& basePath)
{
	string escapedFilename = Util::EscapePath(filename);
	ostringstream oss;
	oss << "{\n  \"OriginFile\": \"" + escapedFilename + "\",\n";
	oss << "  \"SplitFiles\": [\n";

	for (int i = 0; i < files.size(); ++i) {
		oss << "    \"" << Util::EscapePath(files[i]) << "\"";
		if (i < files.size() - 1)
			oss << ",";
		oss << "\n";
	}

	oss << "  ]\n}";

	string filePath = basePath + "/scene.json";
	ofstream out(filePath.c_str());
	if (out) {
		out << oss.str();
		out.close();
	}
	else {
		cerr << "无法写入文件: " << filePath << endl;
	}
}

string SplitFbx::ExportChildFbx(vector<vector<FbxNode*>>& partitionedGroups, FbxManager* partManager, FbxScene* newScene, const string& outputPath, int index, bool useAscii) {

	// 导出每个分组到文件
	if (!partitionedGroups[index].empty()) {
		int fileFormat = useAscii ? partManager->GetIOPluginRegistry()->FindWriterIDByDescription("FBX ascii (*.fbx)") : -1;
		string outputFilePath = outputPath + "/Part_" + to_string(index + 1) + ".fbx";
		FbxExporter* exporter = FbxExporter::Create(partManager, "");
		if (exporter->Initialize(outputFilePath.c_str(), fileFormat, partManager->GetIOSettings())) {
			if (exporter->Export(newScene)) {
				cout << "导出: " << outputFilePath << "（" << partitionedGroups[index].size() << " 个mesh）" << endl;
				return outputFilePath;
			}
			cerr << "导出失败：" << exporter->GetStatus().GetErrorString() << endl;
		}
		else {
			cerr << "导出失败：" << exporter->GetStatus().GetErrorString() << endl;
		}
		exporter->Destroy();
	}
	return "";
}

void SplitFbx::process(const string& inputPath, const string& outputPath, int numParts)
{
	vector<string> files = Split(inputPath, outputPath, numParts);
	//GenerateJsonFileList(files, inputPath, outputPath);
}

void SplitFbx::GetAllMeshNodes(const string& inputPath, vector<FbxNode*>& meshNodes, FbxManager*& manager, FbxScene*& scene) {
	ReadFbx(inputPath, manager, scene);
	GetAllMeshNodes(scene->GetRootNode(), meshNodes);
}

void SplitFbx::ReadFbx(const string& inputPath, FbxManager*& manager, FbxScene*& scene) {
	// 初始化 FBX 管理器
	manager = FbxManager::Create();
	FbxIOSettings* iosSettings = FbxIOSettings::Create(manager, IOSROOT);
	iosSettings->SetBoolProp(EXP_FBX_EMBEDDED, true);
	manager->SetIOSettings(iosSettings);

	// 导入 FBX 文件
	FbxImporter* importer = FbxImporter::Create(manager, "");
	scene = FbxScene::Create(manager, "MainScene");

	if (!importer->Initialize(inputPath.c_str(), -1, manager->GetIOSettings()) || !importer->Import(scene)) {
		cerr << "FBX加载失败\n";
	}
	importer->Destroy();
}